package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.CheckItemDao;
import com.itheima.dao.PermissionDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.Permission;
import com.itheima.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Properties;

/**
 * 权限服务层接口实现类
 */
@Service(interfaceClass = PermissionService.class)
public class PermissionServiceImpl implements PermissionService {
    @Autowired
    private PermissionDao permissionDao;

    //添加权限信息
    public void add(Permission permission) {
        permissionDao.add(permission);
    }

    //分页查询
    public PageResult pageQuery(QueryPageBean queryPageBean) {

        String queryString = queryPageBean.getQueryString();
        Integer pageSize = queryPageBean.getPageSize();
        Integer currentPage = queryPageBean.getCurrentPage();

        PageHelper.startPage(currentPage,pageSize);
        Page<Permission> page=permissionDao.selectByCondition(queryString);
        return new PageResult(page.getTotal(),page.getResult());
    }

    //根据id查询权限信息
    public Permission findById(Integer id) {
        return permissionDao.findById(id);
    }

    //修改权限信息
    public void edit(Permission permission) {
        permissionDao.edit(permission);
    }

    //删除权限
    public void delete(Integer id) {
        long count = permissionDao.findCountByPermissionId(id);
        if(count>0){
            throw new RuntimeException("当前检查项被引用不能被删除");
        }
        permissionDao.deleteById(id);
    }

    @Override
    public List<Permission> findAll() {
        return permissionDao.findAll();
    }
}
